package com.yang.myoschina.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.thoughtworks.xstream.XStream;
import com.yang.myoschina.R;
import com.yang.myoschina.api.remote.OsChina;
import com.yang.myoschina.bean.News;
import com.yang.myoschina.bean.NewsDetail;

import cz.msebera.android.httpclient.Header;

public class NewsDetailActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private TextView tvNewsTitle;
    private WebView tvNewsBody;
    private String newsId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        initUI();
        newsId = getIntent().getStringExtra("NewsId");
        Log.d("MyTag","NewsId:"+newsId);
        OsChina.getNewsDetail(newsId, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String resp = new String(responseBody);
                Log.d("MyTag","新闻详情："+resp);
                XStream xStream = new XStream();
                xStream.processAnnotations(new Class[]{NewsDetail.class,News.class});
                NewsDetail newsDetail = (NewsDetail)xStream.fromXML(resp);
                progressBar.setVisibility(View.GONE);
                tvNewsTitle.setText(newsDetail.getNews().getTitle());
                tvNewsTitle.setVisibility(View.VISIBLE);
                tvNewsBody.loadDataWithBaseURL(null,newsDetail.getNews().getBody(),"text/html","utf-8",null);
                tvNewsBody.setVisibility(View.VISIBLE);
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            }
        });
    }

    private void initUI() {
        progressBar = (ProgressBar)findViewById(R.id.pbProgressBar);
        tvNewsTitle = (TextView) findViewById(R.id.tvNewsTitle);
        tvNewsBody = (WebView) findViewById(R.id.tvNewsBody);
    }
}
